<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notifikasi extends CI_Controller{
	var $_public_view;

	function __construct(){
		parent::__construct();
		$this->load->helper('url');
        $this->load->library('session');
        $this->config->load('ojexcom');
        $this->_public_view = $this->config->item('public_view');
	}

	function index(){

	}

	function get(){
		//cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }
        

		$data_output['success'] = "true";
		$data_output['notifications'] = "";
		$query = $this->db->query("SELECT DISTINCT n.id AS notification_id, n.need_response, s.`status`,s.timeStamp, t.* FROM notifications n JOIN transactionstatus s ON n.transactionStatusId=s.id JOIN `transaction` t ON s.idTransaction=t.id WHERE n.status = 0 AND n.userId = ".$id." order by s.timeStamp desc");
		if($query->num_rows() > 0){
			$row_notif = $query->row(); 
			$count = 0;
			foreach($query->result() as $row_notif){
				if($row_notif->userId > 0 && $row_notif->providerId > 0){
					$data_output['notifications']['transaction'][$count]['notification_id'] = $row_notif->notification_id;
					$data_output['notifications']['transaction'][$count]['timeStamp'] = $row_notif->timeStamp;
					$data_output['notifications']['transaction'][$count]['need_response'] = $row_notif->need_response;
					$data_output['notifications']['transaction'][$count]['idTransaction'] = $row_notif->id;
					$data_output['notifications']['transaction'][$count]['userId'] = $row_notif->userId;
					$data_output['notifications']['transaction'][$count]['userName'] = $this->getNameById($row_notif->userId);
					$data_output['notifications']['transaction'][$count]['userAvatar'] = $this->getAvatarById($row_notif->userId);
					$data_output['notifications']['transaction'][$count]['userLastUpdatedAvatar'] = $this->getLastUpdatedAvatar($row_notif->userId);
					$data_output['notifications']['transaction'][$count]['providerId'] = $row_notif->providerId;
					$data_output['notifications']['transaction'][$count]['providerName'] = $this->getNameById($row_notif->providerId);
					$data_output['notifications']['transaction'][$count]['providerAvatar'] = $this->getAvatarById($row_notif->providerId);
					$data_output['notifications']['transaction'][$count]['providerLastUpdatedAvatar'] = $this->getLastUpdatedAvatar($row_notif->providerId);
					//if($row_notif->need_response == 1){
						$data_output['notifications']['transaction'][$count]['departTime'] = $row_notif->departTime;
						$data_output['notifications']['transaction'][$count]['source_label'] = $row_notif->source_label;
						$data_output['notifications']['transaction'][$count]['source_latitude'] = $row_notif->source_latitude;
						$data_output['notifications']['transaction'][$count]['source_longitude'] = $row_notif->source_longitude;
						$data_output['notifications']['transaction'][$count]['destination_label'] = $row_notif->destination_label;
						$data_output['notifications']['transaction'][$count]['destination_latitude'] = $row_notif->destination_latitude;
						$data_output['notifications']['transaction'][$count]['destination_longitude'] = $row_notif->destination_longitude;
					//}
					$data_output['notifications']['transaction'][$count]['price'] = $row_notif->price;
					$data_output['notifications']['transaction'][$count]['paymentMethod'] = $row_notif->paymentMethod;
					$data_output['notifications']['transaction'][$count]['status'] = $row_notif->status;
					$count++;
				}
			}	
		}
		$query_premium = $this->db->query("SELECT * FROM notifications WHERE type LIKE 'P' AND status = 0 AND userId =  ".$id);
		if($query_premium->num_rows() > 0){
			$row_premium = $query_premium->row(); 
			$data_output['notifications']['premium'][0]['notification_id'] = $row_premium->id;
			$data_output['notifications']['premium'][0]['need_response'] = $row_premium->need_response;
			$data_output['notifications']['premium'][0]['userId'] = $row_premium->userId;
			$data_output['notifications']['premium'][0]['userName'] = $this->getNameById($row_premium->userId);
		}
		//header('Content-Type: application/json');
		//echo json_encode($data_output);
		$data = array(
				'output' => $data_output,
				'page_title' => 'Notifikasi',
				'page_content' => 'notifikasi',
				'isLogin' => $isLogin, 'user'=>$user, 'id' => $id
					);
		$this->load->view($this->_public_view, $data);
	}
	
	function countNotif(){
		//cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }
		$query = $this->db->query("SELECT DISTINCT n.id AS notification_id, n.need_response, s.`status`,s.timeStamp, t.* FROM notifications n JOIN transactionstatus s ON n.transactionStatusId=s.id JOIN `transaction` t ON s.idTransaction=t.id WHERE n.status = 0 AND n.userId = ".$id." order by s.timeStamp desc");
		$count = -1;
		if($query->num_rows() > 0){
			$row_notif = $query->row(); 
			$count++;
			foreach($query->result() as $row_notif){
				if($row_notif->userId > 0 && $row_notif->providerId > 0){
					$count++;
				}
			}
		}
		$query_premium = $this->db->query("SELECT * FROM notifications WHERE type LIKE 'P' AND status = 0 AND userId =  ".$id);
		if($query_premium->num_rows() > 0){
			$count++;
		}
		echo $count;
	}

	function read(){
		//cek segment
        $str_id = '';
        if ($this->uri->segment(3) == FALSE) {
            redirect('home', 'location');
        } else {
            $str_id = $this->uri->segment(3);
        }

		$sql = "UPDATE notifications set status = 1 WHERE id = ? AND need_response = 0";
		//$this->db->query($sql, $this->input->post('notificationsId'));
		$this->db->query($sql,$str_id);

		if($this->db->affected_rows() < 1){
			$data_output['success'] = "false";
			// header('Content-Type: application/json');
			// echo json_encode($data_output);
			$this->get();
		}else{
			$this->get();
		}
	}
	
	function getNameById($userid){
		$query = $this->db->query("SELECT `name` FROM `user`WHERE `id`=".$userid);
		$row = $query->row();
		return $row->name;
	}
	
	function getAvatarById($userid){
		$query = $this->db->query("SELECT `avatar` FROM `user`WHERE `id`=".$userid);
		$row = $query->row();
		return $row->avatar;
	}
	
		
	function getLastUpdatedAvatar($userid){
		$query = $this->db->query("SELECT `lastUpdatedAvatar` FROM `user` WHERE `id`=".$userid."");
		$row = $query->row();
		return $row->lastUpdatedAvatar;
	}
	
}
